<template>
    <div class="tourist_flow" style="padding: 1.1rem 0 0.34rem;">
        <div class="common_header" style="padding-bottom: 10px">
            <div class="header-icon">
                <img src="../assets/imgs/nav_bar_return@2x.png" alt="" class="btn-slide-bar" @click="$router.go(-1)">
                <img src="../assets/imgs/nav_bar_more@2x.png" alt="" style="position: relative;z-index: 3;"
                     @click="showShare">
            </div>
            <div class="header-title head-title-pdf">{{title}}</div>
        </div>
        <div class="content">
            <div class="pdf-tab">
                <div
                        class="btn-def btn-pre"
                        @click.stop="prePage">
                    <van-icon name="arrow-up" />
                </div>
                <div
                        class="btn-def btn-next"
                        @click.stop="nextPage">
                    <van-icon name="arrow-down" />
                </div>
            </div>
            <pdf
                    ref="pdf"
                    :src="pdfUrl"
                    :page="pageNum"
                    @password="password"
                    @progress="loadedRatio = $event"
                    @page-loaded="pageLoaded($event)"
                    @num-pages="pageTotalNum=$event"
                    @error="pdfError($event)"
                    @link-clicked="page = $event">
            </pdf>
        </div>
        <van-actionsheet v-model="showShares">
            <div class="sheet">
                <div class="sheet-list">
                    <span class="sheet-title" @click="downPdf">
                        <img src="../assets/imgs/report_page_pdf@2x.png" class="sheet-img" alt="">
                        <span>下载pdf</span>
                    </span>
                    <span class="sheet-title" @click="createImage">
                        <img src="../assets/imgs/report_page_img@2x.png" class="sheet-img" alt="">
                        <span>生成图片</span>
                    </span>
                    <span class="sheet-title" @click="shareFirend">
                        <img src="../assets/imgs/report_page_wechat@2x.png" class="sheet-img" alt="">
                        <span>微信好友</span>
                    </span>
                    <span class="sheet-title" @click="shareFirendQuan">
                        <img src="../assets/imgs/report_page_friends@2x.png" class="sheet-img" alt="">
                        <span>朋友圈</span>
                    </span>

                </div>
                <div class="cancel" @click="showShares=false">取消</div>
            </div>
        </van-actionsheet>
        <div class="show-img">
            <van-popup v-model="showImage">
                <div class="img-popup">
                    <div class="img-popup-img">
                        <van-icon name="cross" class="clear" @click="showImage=false"/>
                        <div style="text-align: center;padding: 0.3rem 0 0.2rem;font-weight: 600;font-size: 0.18rem">
                            {{title}}
                        </div>
                        <div class="img">
                            <img :src="imgUrl" style="display: block;" alt="">
                        </div>
                    </div>
                    <div class="img-popup-icon">
                        <span @click="saveImage" class="sheet-title">
                        <img src="../assets/imgs/report_page_img@2x.png" class="sheet-img" alt="">
                        <span>保存到相册</span>
                    </span>
                        <span class="sheet-title" @click="shareFirend">
                        <img src="../assets/imgs/report_page_wechat@2x.png" class="sheet-img" alt="">
                        <span>微信好友</span>
                    </span>
                        <span class="sheet-title" @click="shareFirendQuan">
                        <img src="../assets/imgs/report_page_friends@2x.png" class="sheet-img" alt="">
                        <span>朋友圈</span>
                    </span>
                    </div>
                </div>
            </van-popup>
        </div>

    </div>
</template>
<script>
    import {Notify, Loading, List,Actionsheet,Toast,Icon } from 'vant';
    // import PDFJS from 'pdfjs-dist'
    import pdf from 'vue-pdf'
    var wx = api.require('wx');
    // const Base64 = require('js-base64').Base64;
    export default {
        name: "index",
        components: {
            pdf,
            [Notify.name]: Notify,
            [Icon.name]: Icon,
            [Toast.name]: Toast,
            [Actionsheet.name]: Actionsheet,
            [List.name]: List,
            [Loading.name]: Loading
        },
        data() {
            return {
                showImage: false,
                showShares: false,
                pageNum: 1,
                title: '',
                pdfUrl: '',
                pdfUrlDown: '',
                imgUrl: '',
                pdf_id: null,
                numPages: 1,
                isShowPdf: false
            };
        },
        mounted() {
            this.title = this.$route.query.filename;
            this.pdfUrl = this.$route.query.url;
            this.pdfUrlDown = this.$route.query.url;
            this.imgUrl = this.$route.query.imgUrl;
            this.pdf_id = this.$route.query.id;
            // this.downLoadImage();
            this.pdfTask(this.$route.query.url);
        },
        methods: {
            prePage(){
                var p = this.pageNum
                p = p>1?p-1:this.pageTotalNum
                this.pageNum = p
            },
            nextPage(){
                var p = this.pageNum
                p = p<this.pageTotalNum?p+1:1
                this.pageNum = p
            },
            password(updatePassword, reason) {
                updatePassword(prompt('password is "123456"'))
                console.log('...reason...')
                console.log(reason)
                console.log('...reason...')
            },
            pageLoaded(e){
                this.curPageNum = e
            },
            pdfError(error){
                console.error(error)
            },
            downLoadImage(){
                let self = this;
                wx.isInstalled(function (ret, err) {
                    if (ret.installed) {
                        // let timestamp = new Date().getTime()
                        api.download({
                            url: self.imgUrl, // 图片地址
                            savePath: 'fs://test' + this.pdf_id + '.jpg', // 图片存储路径
                            report: true,
                            cache: true,
                            allowResume: true
                        }, function (res, err) {
                            if (res.state == 1) {
                                Toast('图片缓存成功');
                                console.info('图片缓存',res);
                            } else {
                                Toast(err.msg)
                            }
                        })
                    } else {
                        alert('当前设备未安装微信客户端');
                    }
                });
            },
            pdfTask(pdfUrl) {
                this.$store.commit('showLoading');
                let self = this;
                let loadingTask = pdf.createLoadingTask(pdfUrl)
                loadingTask.then(pdf => {
                    console.info('pdf',pdf);
                    console.info('loadingTask',loadingTask);
                    self.pdfUrl = loadingTask;
                    self.numPages = pdf.numPages;
                    this.$store.commit('hideLoading');
                }).catch((err) => {
                    this.$store.commit('hideLoading');
                    Toast('pdf加载失败');
                })
            },
            saveImage() {
                let save_link = document.createElementNS('http://www.w3.org/1999/xhtml', 'a');
                save_link.href = this.imgUrl;
                save_link.download = this.title;
                let event = document.createEvent('MouseEvents');
                event.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
                save_link.dispatchEvent(event);
            },
            shareFirend() {
                // wx.shareImage({
                //     apiKey: 'wx2ade05388652ed08',
                //     scene: 'session',
                //     thumb: 'widget://res/logo.png', // 不知道为什么这里使用fs:// 死活都有问题，无法分享
                //     contentUrl: 'fs://test' + this.pdf_id + '.jpg'
                // }, function (ret, err) {
                //     if (ret.status) {
                //         Toast('分享成功')
                //     } else {
                //         Toast(err.code)
                //     }
                // })
                let self = this;
                wx.isInstalled(function (ret, err) {
                    if (ret.installed) {
                        wx.shareWebpage({
                            // apiKey: 'wx504b83edf8bec631',
                            apiKey: 'wx1724ed003e290b12',
                            scene: 'session',
                            title: '岳圣科技',
                            description: '岳圣科技分享图',
                            thumb: 'widget://res/logo.png', // 不知道为什么这里使用fs:// 死活都有问题，无法分享
                            contentUrl: self.imgUrl
                        }, function (ret, err) {
                            if (ret.status) {
                                Toast('分享成功');
                            } else {
                                Toast(err.code);
                            }
                        });
                    } else {
                        alert('当前设备未安装微信客户端');
                    }
                })
            },
            shareFirendQuan() {
                let self = this;
                wx.isInstalled(function (ret, err) {
                    if (ret.installed) {
                        wx.shareWebpage({
                            // apiKey: 'wx504b83edf8bec631',
                            apiKey: 'wx1724ed003e290b12',
                            scene: 'timeline',
                            title: '岳圣科技',
                            description: '岳圣科技分享图',
                            thumb: 'widget://res/logo.png', // 不知道为什么这里使用fs:// 死活都有问题，无法分享
                            contentUrl: self.imgUrl
                        }, function (ret, err) {
                            if (ret.status) {
                                Toast('分享成功');
                            } else {
                                Toast(err.code);
                            }
                        });
                    } else {
                        alert('当前设备未安装微信客户端');
                    }
                })
                // wx.shareImage({
                //     apiKey: 'wx2ade05388652ed08',
                //     scene: 'timeline',
                //     thumb: 'widget://res/logo.png', // 不知道为什么这里使用fs:// 死活都有问题，无法分享
                //     contentUrl: 'fs://test' + this.pdf_id + '.jpg'
                // }, function (ret, err) {
                //     if (ret.status) {
                //         Toast('分享成功');
                //     } else {
                //         Toast(err.code);
                //     }
                // });
            },
            showShare(val) {
                this.showShares = true;
            },
            downPdf() {
                // console.info('this.pdfUrlDetail',this.pdfUrl);
                window.location.href = this.pdfUrlDown;
            },
            createImage() {
                this.showShares = false;
                this.showImage = true;
            } //生成图片
        }
    };
</script>

<style scoped lang="less">
    .pdf-tab{
        position: fixed;
        bottom: 10px;
        right: 10px;
        .btn-def{
            width: 0.4rem;
            height: 0.4rem;
            background-color: rgba(0,0,0,0.2);
            border-radius: 50%;
            display: flex;
            justify-content: center;
            align-items: center;
            font-size: 0.2rem;
            color: rgba(255,255,255,.8);
            margin-bottom: 20px;
        }
    }
    canvas {
        display: block;
        border-bottom: 1px solid black;
    }
    .custom-tabs {
        /*height: 55px;*/
        padding: 0 20px;
        border-bottom: 0.5px solid #f6f6f6;
    }

    .custom-tab-item {
        display: inline-block;
        line-height: 0.44rem;
        height: 0.44rem;
        margin-right: 0.3rem;
        color: rgba(40, 44, 52, 1);
        cursor: pointer;
    }

    .custom-tab-item.active {
        font-weight: 700;
        color: rgba(40, 44, 52, 1);
        border-bottom: 2px solid #4588d4;
    }

    .custom-tab-content {
        padding-top: 60px;
        height: 100%;
        /* position: relative; */
    }
</style>
<style scoped lang="less">
    .header-icon {
        display: flex;
        justify-content: space-between;
        padding-top: 0.16rem;
        padding: 0.16rem 0.18rem 0;
        img {
            width: 0.18rem;
            height: 0.18rem;
            color: rgba(255, 255, 255, 0.7);
        }
    }
    .tourist_flow {
        padding: 1.4rem 0 0.34rem;
    }
.sheet{
    .cancel{
        text-align: center;
        color:rgba(40,44,52,1);
        font-size: 0.14rem;
        line-height: 60px;
        border-top: 1px solid rgba(0,0,0,0.03);
    }
    .sheet-list{
        padding-left: 0.04rem;
        padding-bottom: 0.15rem;
        display: flex;
        .sheet-title{
            display: flex;
            flex-direction: column;
            align-items: center;
        }
        .sheet-img{
            width: 0.44rem;
            margin: 0.14rem 0.14rem 0.06rem;
        }
    }

}
    .content_pian {
        width: 3.43rem;
        box-sizing: border-box;
        background: rgba(255, 255, 255, 1);
        box-shadow: 0rem 0.08rem 0.4rem 0rem rgba(119, 136, 175, 0.2);
        border-radius: 0.08rem;
        margin: 0.2rem auto 0;
        padding: 0.16rem;
        .content-name {
            overflow: hidden;
            text-overflow: ellipsis; //超出部分以省略号显示
            white-space: nowrap;
            font-weight: bold;
            color: rgba(40, 44, 52, 1);
            font-size: 0.18rem;
            padding-bottom: 0.15rem;
        }
        .content-des {
            display: flex;
            justify-content: space-between;
            color: rgba(172, 178, 199, 1);
            font-size: 0.12rem;
            line-height: 0.16rem;
            .img {
                width: 0.14rem;
                vertical-align: middle;
            }
            .img1 {
                margin-left: 0.2rem;
                width: 0.14rem;
                vertical-align: middle;
            }
        }
    }
</style>


